import UI from "../common/UI";
import { LevelManager } from "./LevelManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettlePanel extends cc.Component {
    private levelInfo: any[] = []

    private curIdiomIdx: number = 0
    private contentNode: cc.Node

    protected onLoad(): void {
        this.contentNode = UI.Child(this.node, 'content')
        UI.OnClick(this.node, 'back', function () {
            cc.director.loadScene('main', null)
        }, this)

        UI.OnClick(this.node, 'next', function () {
            LevelManager.curLevel = LevelManager.curLevel + 1
            cc.director.loadScene('idiom_game', null)
        }, this)

        this.activeIdxButton()
        UI.OnClick(this.contentNode, 'left', function () {
            this.curIdiomIdx--
            this.label()
            this.activeIdxButton()
        }, this)

        UI.OnClick(this.contentNode, 'right', function () {
            this.curIdiomIdx++
            this.label()
            this.activeIdxButton()
        }, this)
    }

    public init(levelInfo: any[]) {
        this.levelInfo = levelInfo
        this.label()
    }

    private label() {
        this.contentNode = UI.Child(this.node, 'content')
        UI.Label(this.contentNode, 'title', this.levelInfo[this.curIdiomIdx]['text'])
        UI.Label(this.contentNode, 'layout/pinyin', '拼音:' + this.levelInfo[this.curIdiomIdx]['pinyin'])
        UI.Label(this.contentNode, 'layout/derivation', '来源:' + this.levelInfo[this.curIdiomIdx]['derivation'])
        UI.Label(this.contentNode, 'layout/explanation', '释义:' + this.levelInfo[this.curIdiomIdx]['explanation'])
        UI.Label(this.contentNode, 'layout/example', '示例:' + this.levelInfo[this.curIdiomIdx]['example'])
    }

    private activeIdxButton() {
        UI.Active(this.contentNode, 'left', this.curIdiomIdx != 0)
        UI.Active(this.contentNode, 'right', this.curIdiomIdx != this.levelInfo.length - 1)
    }
}
